<?php
// +----------------------------------------------------------------------
// | 商标属性分类
// +----------------------------------------------------------------------
// | Copyright (c) 2015
// +----------------------------------------------------------------------
// | Author: baiping 125618036@qq.com http://www.webipcode.com
// +----------------------------------------------------------------------
// |2015-11-17 下午5:10:36
// +----------------------------------------------------------------------

class Trademark_attr_model extends CI_Model{
	
	private $table_name;
	public function __construct(){
		parent::__construct();
		$this->table_name = 'trademark_attr';
	}
	
	/**
	 * 执行wher条件
	 * @param unknown_type $where
	 * @author Baip
	 */
	private function _where($where){
		if ( is_array( $where ) ){
			foreach ( $where as $key => $val ){
				if ( is_array( $val ) ){
					$this->db->where_in($key, $val);
					unset($where[$key]);
				}else{
					$this->db->where($key, $val);
				}
			}
		}
		return true;
	}
	
	/**
	 * 添加数据
	 */
	public function addData(){
		$data_arr = $this->postData();
		$query = $this->db->insert($this->table_name, $data_arr);
		return $this->db->insert_id();
	}
	
	/**
	 * post接收值
	 */
	private function postData(){
		$title          = $this->input->post('title',true);//商标属性名称
		$catid          = $this->input->post('catid',true);//商标分类
		$thumb          = $this->input->post('thumb',true);//商标缩略图
		$thumb_id       = $this->input->post('thumb_id',true);//商标缩略图id
		$service_price  = $this->input->post('service_price',true);//服务费
		$officer_price  = $this->input->post('officer_price',true);//官费
		$server_intro   = $this->input->post('server_intro',true);//服务介绍
		$process        = $this->input->post('process',true);//办理流程
		$required_information  = $this->input->post('required_information',true);//所需资料
		$problem  = $this->input->post('problem',true);//常见问题
		$recommend  = $this->input->post('recommend',true);//是否推荐
	
		//产生随机字符串
		$data_arr['title'] = $title;
		$data_arr['thumb'] = $thumb;
		$data_arr['thumb_id'] = $thumb_id;
		$data_arr['cat_id'] = $catid;
		$data_arr['server_intro'] = $server_intro;
		$data_arr['process'] = $process;
		$data_arr['required_information'] = $required_information;
		$data_arr['problem'] = $problem;
		$data_arr['service_price'] = $service_price;
		$data_arr['officer_price'] = $officer_price;
		$data_arr['recommend'] = intval($recommend) == 1 ? 1 : 0;
		$data_arr['addtime'] = time();
		return $data_arr;
	}
	
	
	/**
	 * 获取数据部数
	 * @param unknown_type $where
	 * @param unknown_type $field
	 */
	public function getCount($where=array(), $field="id"){
		$count =  $this->db->where($where)->select($field)->count_all_results($this->table_name);
		return $count;
	}
	
	/**
	 * 获取数据
	 * @param array $where 获取条件
	 * @param string $field 获取字段
	 * @param string $limit 获取数量
	 * @param string $order 排序
	 * @author Baip
	 * @deprecated:limit的传值为（每页记录数,偏移数）生成的sql语句为 limit(偏移数,记录数)
	 */
	public function getData($where=array(), $field="id,title,service_price,officer_price", $limit=15, $offset=0,  $order="id DESC"){
		$res = $this->db->select($field)->where($where)->order_by($order)->limit($limit, $offset)->get($this->table_name);
// 		echo $this->db->last_query();
		return $res->result();
	}
	/**
	 * 左关联商品分类表获取数据
	 * @param array $where 获取条件
	 * @param string $field 获取字段
	 * @param string $limit 获取数量
	 * @param string $order 排序
	 * @author Baip
	 * @deprecated:limit的传值为（每页记录数,偏移数）生成的sql语句为 limit(偏移数,记录数)
	 */
	public function getDataByJoin($where=array(), $field="b.id,b.title,b.service_price,b.officer_price,a.title", $limit=15, $offset=0,  $order="id DESC"){
		 $this->_where($where);
		 $this->db->from($this->table_name .' as b');
		 $this->db->join('catgory as a', 'a.id = b.cat_id');
		 $this->db->select($field);
		 $this->db->order_by($order);
		 $this->db->limit($limit, $offset);
		 $res = $this->db->get();
// 		 var_dump( $this->db->last_query() );
		return $res->result();
	}
	
	/**
	 * 删除数据
	 * @param array/string $where 删除条件
	 */
	public function deleteData($where){
		if ( empty( $where ) ){
			return false;
		}
		
		$this->_where($where);
		$query = $this->db->delete($this->table_name);
		return $query;
	}
	
	/**
	 * 查询单条数据
	 * @param array/string $where　查询条件
	 * @param string $field 字段名
	 */
	public function getFind($where, $field="*"){
		if( empty( $where ) ) return false;
		$this->_where($where);
		$this->db->select($field);
		$this->db->limit(1);
		$this->db->from($this->table_name);
		$res = $this->db->get();
		if ( $res->num_rows() > 0 ){
			return $res->row();
		}else{
			return false;
		}
	}
	
	/**
	 * 更新数据
	 * @param unknown_type $where
	 */
	public function updateData($where){
		if ( empty( $where ) ){
			return false;
		}
		$data_arr = $this->postData();
		$query = $this->db->where($where)->update($this->table_name,$data_arr);
		return $query;
	}
	
	/**
	 * 更新商标状态（可以传递更新数据）
	 * @param unknown_type $where
	 * @param unknown_type $data
	 */
	public function updateState($where, $data){
		if ( empty( $where ) ){
			return false;
		}
		if ( !is_array( $data ) ) return false;
		$this->_where($where);	
		$querey = $this->db->update($this->table_name, $data);
		return $querey;	
	}
}